package com.coc.dao;

import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class BuildSettingXML {
	protected static final Log logger = LogFactory
			.getLog(BuildSettingXML.class);
	private static Hashtable setMap;
	private static List<Building> treeList;
	public  int GlodPerGem=20;
	public  int ElxmPerGem=20;
	public  int DakmPerGem=10;
	public  int MSPerGem=150000;
	public int reload() {
		try {
			setMap = null;
			treeList =null;
			praseXML();
			return 1;
		} catch (Exception e) {
			logger.error("SetUpdateXML--reload", e);
		}
		return 0;
	}

	public int findIntByID(String id) {
		if (setMap == null) {
			praseXML();
		}
		if (!setMap.containsKey(id)) {
			return 0;
		}
		return Integer.parseInt((String) setMap.get(id));
	}

	public Building findBuByID(String id) {
		if (setMap == null) {
			praseXML();
		}
		if (!setMap.containsKey(id)) {
			return null;
		}
		return (Building) setMap.get(id);
	}
	public Building findRandomTree() {
		if (treeList == null) {
			praseXML();
		}
		Random r = new Random();
		int pos = r.nextInt(treeList.size());
		return treeList.get(pos);
	}
	public Troops findTrByID(String id) {
		if (setMap == null) {
			praseXML();
		}
		if (!setMap.containsKey(id)) {
			return null;
		}
		return (Troops) setMap.get(id);
	}

	public String findByID(String id) {
		if (setMap == null) {
			praseXML();
		}
		if (!setMap.containsKey(id)) {
			return null;
		}
		return (String) setMap.get(id);
	}

	private String getValue(String key, Element elmmap) {
		if (elmmap.attribute(key) != null) {
			return elmmap.attribute(key).getText();
		}
		return "0";
	}

	public void praseXML() {
		if (setMap != null)
			return;
		setMap = new Hashtable();
		treeList = new ArrayList();
		try {
			URL url = new URL("http://219.239.223.50/Building.xml");
			SAXReader reader = new SAXReader();
			Document document = reader.read(url);
			Element rootElm = document.getRootElement();
			Element building = rootElm.element("buildings");

			for (Iterator itm = building.elementIterator(); itm.hasNext();) {
				Element elmmap = (Element) itm.next();

				String type = elmmap.getName();
				//树和装饰,每个lv子项是不同的东西,直接拆分成一个个大项,带一个lv1的list
				if (type.equals("DECT") || type.equals("TREE")) {
					
					String sold = "";
					if (elmmap.attribute("sold") != null) {
						sold=elmmap.attribute("sold").getText();
					}
					List list = new ArrayList();
					List lvs = elmmap.elements();
					for (Iterator lv = lvs.iterator(); lv.hasNext();) {
						
						elmmap = (Element) lv.next();
						String subname = elmmap.getName();
						Building bu = new Building();
						bu.setSold(sold);
						bu.setType(subname);
						bu.setCount("");
						String size = getValue("size", elmmap);
						String cost = getValue("cost", elmmap);
						String time = getValue("time", elmmap);
						String htlv = getValue("lv", elmmap);
						String max = getValue("max", elmmap);
						bu.setSize(size);
						list.add(1 + ";" + htlv + ";" + cost + ";" + time
								+ ";" + max + ";" + ";" + ";");
						bu.setLv(list);
						setMap.put(subname, bu);
						if(type.equals("TREE")){
							treeList.add(bu);
						}
						
					}
					
					//几种炸弹是没LV子项的
					//强制设置lv1的list
				}else if (type.equals("BOMB") || type.equals("TRAP") || type.equals("GITB") || type.equals("AIRB") || type.equals("AIRM")) {
					Building bu = new Building();
					bu.setType(type);
					bu.setCount(elmmap.attribute("max").getText());
					if (elmmap.attribute("sold") != null) {
						bu.setSold(elmmap.attribute("sold").getText());
					}else{
						bu.setSold("");
					}
					List list = new ArrayList();
					String htlv = getValue("th", elmmap);
					String cost = getValue("cost", elmmap);
					String time = getValue("time", elmmap);
					// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
					list.add(1 + ";" + htlv + ";" + cost + ";" + time
							+ ";" + "0" + ";" + "0" + ";" + "0" + ";"
							+ "0");
					bu.setLv(list);
					setMap.put(type, bu);
				} else {
					String count = "";
					if (elmmap.attribute("max") != null) {
						count = elmmap.attribute("max").getText();
					}
					String size = elmmap.attribute("size").getText();
					if (size.startsWith("-")) {
						size = size.substring(1);
					}
					
					int boost_rate = 1;
					if (elmmap.attribute("boost_rate") != null) {
						boost_rate = Integer.parseInt(elmmap.attribute(
								"boost_rate").getText());
					}
					int time_boost = 1;
					if (elmmap.attribute("time_boost") != null) {
						time_boost = Integer.parseInt(elmmap.attribute(
								"time_boost").getText());
					}

					Building bu = new Building();
					if (elmmap.attribute("sold") != null) {
						bu.setSold(elmmap.attribute("sold").getText());
					}else{
						bu.setSold("");
					}
					bu.setType(type);
					bu.setCount(count);
					bu.setSize(size);
					bu.setSpeedboos(time_boost);
					bu.setBoostrate(boost_rate);
					List list = new ArrayList();
					List lvs = elmmap.elements("lv");
					for (Iterator lv = lvs.iterator(); lv.hasNext();) {
						elmmap = (Element) lv.next();
						String id = getValue("id", elmmap);
						String htlv = getValue("th", elmmap);
						String cost = getValue("cost", elmmap);
						String time = getValue("time", elmmap);
						String capt = getValue("cap", elmmap);
						String speed = getValue("speed", elmmap);
						String exp = getValue("exp", elmmap);
						String boost = getValue("boost", elmmap);
						// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
						list.add(id + ";" + htlv + ";" + cost + ";" + time
								+ ";" + capt + ";" + speed + ";" + exp + ";"
								+ boost);
					}
					bu.setLv(list);
					setMap.put(type, bu);
				}
			}
			Element SPELL = rootElm.element("troops").element("SPEL");
			for (Iterator itm = SPELL.elementIterator(); itm.hasNext();) {
				Element elmmap = (Element) itm.next();
				String type = elmmap.getName();
				int time = Integer.parseInt(elmmap.attribute("time_train")
						.getText());
				String maker = elmmap.attribute("maker").getText();
				Troops tr = new Troops();
				tr.setMaker(Integer.valueOf(Integer.parseInt(maker)));
				tr.setFromBulid("SPELL");
				tr.setType(type);
				tr.setTime(Integer.valueOf(time));
				List list = new ArrayList();
				List lvs = elmmap.elements("lv");
				for (Iterator lv = lvs.iterator(); lv.hasNext();) {
					elmmap = (Element) lv.next();
					String id = getValue("id", elmmap);
					String lab = getValue("lab", elmmap);

					String cost = getValue("cost", elmmap);
					String cost_up = getValue("cost_up", elmmap);
					String time_up = getValue("time_up", elmmap);
					// 等级;实验室等级;研究费用;升级费用;升级时间
					list.add(id + ";" + lab + ";" + cost + ";" + cost_up + ";"
							+ time_up);
				}
				tr.setLv(list);
				setMap.put(type, tr);
			}
			SPELL = rootElm.element("troops").element("BARK");
			for (Iterator itm = SPELL.elementIterator(); itm.hasNext();) {
				Element elmmap = (Element) itm.next();
				String type = elmmap.getName();
				int time = Integer.parseInt(elmmap.attribute("time_train")
						.getText());
				String maker = elmmap.attribute("maker").getText();
				Troops tr = new Troops();
				tr.setMaker(Integer.valueOf(Integer.parseInt(maker)));
				tr.setSpace(Integer.parseInt(elmmap.attribute("space").getText()));
				tr.setFromBulid("BARK");
				tr.setType(type);
				tr.setTime(Integer.valueOf(time));
				List list = new ArrayList();
				List lvs = elmmap.elements("lv");
				for (Iterator lv = lvs.iterator(); lv.hasNext();) {
					elmmap = (Element) lv.next();
					String id = getValue("id", elmmap);
					String lab = getValue("lab", elmmap);
					String cost = getValue("cost", elmmap);
					String cost_up = getValue("cost_up", elmmap);
					String time_up = getValue("time_up", elmmap);

					list.add(id + ";" + lab + ";" + cost + ";" + cost_up + ";"
							+ time_up);
				}
				tr.setLv(list);
				setMap.put(type, tr);
			}
			SPELL = rootElm.element("troops").element("BKDK");
			for (Iterator itm = SPELL.elementIterator(); itm.hasNext();) {
				Element elmmap = (Element) itm.next();
				String type = elmmap.getName();
				int time = Integer.parseInt(elmmap.attribute("time_train")
						.getText());
				String maker = elmmap.attribute("maker").getText();
				Troops tr = new Troops();
				tr.setMaker(Integer.valueOf(Integer.parseInt(maker)));
				tr.setSpace(Integer.parseInt(elmmap.attribute("space").getText()));
				tr.setFromBulid("BKDK");
				tr.setType(type);
				tr.setTime(Integer.valueOf(time));
				List list = new ArrayList();
				List lvs = elmmap.elements("lv");
				for (Iterator lv = lvs.iterator(); lv.hasNext();) {
					elmmap = (Element) lv.next();
					String id = getValue("id", elmmap);
					String lab = getValue("lab", elmmap);
					String cost = getValue("cost", elmmap);
					String cost_up = getValue("cost_up", elmmap);
					String time_up = getValue("time_up", elmmap);

					list.add(id + ";" + lab + ";" + cost + ";" + cost_up + ";"
							+ time_up);
				}
				tr.setLv(list);
				setMap.put(type, tr);
			}

			SPELL = rootElm.element("troops").element("HERO");
			for (Iterator itm = SPELL.elementIterator(); itm.hasNext();) {
				Element elmmap = (Element) itm.next();
				String type = elmmap.getName();

				if (type.equals("queen")) {
					type = "quen";
				}
				Building bu = findBuByID(type.toUpperCase());
				List list = bu.getLv();
				List lvs = elmmap.elements("lv");
				for (Iterator lv = lvs.iterator(); lv.hasNext();) {
					elmmap = (Element) lv.next();
					String id = getValue("id", elmmap);
					if (!id.equals("1")) {
						String htlv = getValue("th", elmmap);
						String cost = getValue("cost_up", elmmap);
						String time = getValue("time_up", elmmap);

						list.add(id + ";" + htlv + ";" + cost + ";" + time
								+ ";;;;");
					}
				}
				bu.setLv(list);
				setMap.remove(type.toUpperCase());
				setMap.put(type.toUpperCase(), bu);
			}
		} catch (Exception e) {
			setMap = null;
			e.printStackTrace();
		}
	}
}

/*
 * Location: C:\Work\WorkSpace\classes\ Qualified Name:
 * com.coc.dao.BuildSettingXML JD-Core Version: 0.6.2
 */